package com.example.demo.controller;

import com.example.demo.pojo.ChatGroupPO;
import com.example.demo.service.ChatGroupService;
import com.example.demo.service.impl.ChatGroupServiceImpl;
import com.example.demo.utility.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.UUID;

@RestController
@CrossOrigin
public class ChatController {
    @Autowired
    private ChatGroupService chatGroupService;

    //新增聊天群组
    @PostMapping("/ChatGroup/add")
    public ResponseEntity<Result> addChatGroup(@RequestParam String name){
        // 生成 UUID 作为键
        String key = UUID.randomUUID().toString();
        String ChatKey = "chat:" + key;
        ChatGroupPO chatGroupPO = chatGroupService.setChatGroupName(ChatKey,name);
        if (chatGroupPO == null){
            Result result = new Result(404, "新建群组失败", null);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);  // 返回 404 Not Found 响应
        }else {
            Result result = new Result(200, "创建成功", chatGroupPO);
            return ResponseEntity.ok(result);  // 返回 200 OK 响应
        }

    }
    @GetMapping("/ChatGroup/get")
    public ResponseEntity<Result> getChatGroup(){
        ArrayList<ChatGroupPO> chatGroup = chatGroupService.getChatGroup("chat");
        if (chatGroup == null){
            Result result = new Result(404, "查询群聊失败", null);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);  // 返回 404 Not Found 响应
        }else {
            Result result = new Result(200, "查询成功", chatGroup);
            return ResponseEntity.ok(result);  // 返回 200 OK 响应
        }

    }
}
